package org.hagopa.games.moneypool;
import java.util.ArrayList;
import java.util.List;

import org.hagopa.games.moneypool.game.GamesManager;
import org.hagopa.games.moneypool.network.Reception;
import org.hagopa.games.moneypool.network.Server;
import org.hagopa.games.moneypool.policy.FlashSocketPolicySupport;


public class MainClass {
	public static void main(String[] args) {
		FlashSocketPolicySupport fp = null;
		Configuration cfg = Configuration.getInstance();
		if(Configuration.isAllowPoliceInsideServer() == true)
		{
			fp = new FlashSocketPolicySupport(Configuration.getPathToPolicyXml());
			fp.init();
		}
	
		List <Long> reQ = new  ArrayList <Long>();
		Reception Clients =  Reception.getInstance();
		Server newServer = new Server(Configuration.getPort());
		
		
		System.out.println("Starting Server [Main Thread]");
		
		if(newServer.init(Clients)== false)
		{
			newServer.stopServer();
			return;
		}
		newServer.setRequestsQ(reQ);
		
		GamesManager GM = new GamesManager(cfg);
		Thread GMThread = new Thread(GM);
		GM.setRequestsQ(reQ);
		GM.linkWaitingConnections(Clients);
		System.out.println("Starting Games Manager Thread");
		GMThread.start();
		try {
			GMThread.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		newServer.stopServer();
		System.out.println("Server stopped [Main Thread]");
	}
}
